#include <iostream>
#include <vector>
using namespace std;

class App {
	int cnt = 1, qcnt = 1;
	vector<int> head, next, to;
	vector<int> qhead, qnext, qto, qid;
	vector<int> nums, lca, vis, parent;
	vector<int> weight;
	vector<int> father;

	void addedge(int u, int v)
	{
		next[cnt] = head[u];
		to[cnt] = v;
		head[u] = cnt++;
	}

	void addquest(int u, int v, int i)
	{
		qnext[qcnt] = qhead[u];
		qto[qcnt] = v;
		qid[qcnt] = i;
		qhead[u] = qcnt++;
	}

	int find(int x)
	{
		return x == father[x] ? x : father[x] = find(father[x]);
	}

	void tarjan(int u, int p)
	{
		parent[u] = p;
		vis[u] = true;
		for (int e = head[u], v; e; e = next[e]) {
			v = to[e];
			if (v != p) {
				tarjan(v, u);
				for (int q = qhead[v], p; q; q = qnext[q]) {
					p = qto[q];
					if (vis[p]) {
						lca[qid[q]] = find(p);
					}
				}
				father[v] = u;
			}
		}
	}

	void dfs(int u, int p)
	{
		for (int e = head[u], v; e; e = next[e]) {
			v = to[e];
			if (v != p) {
				dfs(v, u);
				weight[u] += weight[v];
			}
		}
	}

    public:
	App(int n)
		: head(n + 1, 0), next(n << 1, 0), to(n << 1, 0), qhead(head), qnext(next), qto(to), qid(to), //
		  nums(head), lca(head), father(head), vis(head), weight(n + 1, 0), parent(head)
	{
		for (int i = 1; i <= n; i++) {
			cin >> nums[i];
			father[i] = i;
		}

		for (int i = 1, u, v; i <= n - 1; i++) {
			cin >> u >> v;
			addedge(u, v);
			addedge(v, u);
		}

		for (int i = 1, u, v; i < n; i++) {
			u = nums[i];
			v = nums[i + 1];
			addquest(u, v, i);
			addquest(v, u, i);
		}

		tarjan(1, 0);

		for (int i = 1, u, v, c; i < n; i++) {
			u = nums[i];
			v = nums[i + 1];
			c = lca[i];
			weight[u] += 1;
			weight[v] += 1;
			weight[c] -= 1;
			weight[parent[c]] -= 1;
		}

		dfs(1, 0);
		for (int i = 2; i <= n; i++) {
			--weight[nums[i]];
		}

                for(int i = 1; i <= n; i++){
                        cout << weight[i] << endl;
                }
	}
};

int main()
{
	int n = 0;
	cin >> n;
	App app(n);
	return 0;
}